//*********************************************************************
// Copyright (C) 2014 Hell Prototypes / www.hellprototypes.com
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
//********************************************************************
#include <avr/io.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "common.h"


void buzzer_init(void )
{
	uint16_t tone = 500;

	TCD1.PER = tone * 2;
	TCD1.CCB = (tone * 2) - (tone / 8);
	//TCD1.CTRLC = 0x02;//Match cmp when timer turn off ( Turn off buzzing MOS)
	TCD1.CTRLB = 0x23;//CCBEN, Single-slope PWM
}

void buzzer_buzz(byte len)
{
	TCD1.CTRLA = 0x04;//DIV8, 2MHz

	ms_delay(len);
	while(!testbit(PORT_BEEP_IN, PIN_BEEPER));//waiting for pin high
	while(testbit(PORT_BEEP_IN, PIN_BEEPER));//waiting for pin low
	TCD1.CTRLA = 0x00;//OFF
}
